package com.neu.filter.wty;


import com.neu.service.wty.UserService;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Map;

/**
 * @author wty
 */
@WebFilter(filterName = "CheckLoginFilter",urlPatterns={"/xxx.jsp"})
public class CheckLoginFilter implements Filter {


    @Override
    public void destroy() {
    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("CheckLoginFilter");
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        HttpSession session = request.getSession();
        Cookie[] cookies = request.getCookies();
        String initUrl = request.getHeader("referer");
        System.out.println(initUrl);
        String user="user";
            if (request.getSession().getAttribute(user) == null &cookies.length<2) {
                request.getRequestDispatcher("login.jsp").forward(request, response);
                chain.doFilter(req, resp);
            }else if (request.getSession().getAttribute(user) != null){
                chain.doFilter(req, resp);
            } else {
                    Cookie c = null;
                    for (Cookie cookie : cookies) {
                        System.out.println(cookie.getName());
                        if ("auto_login".equals(cookie.getName())) {
                            c = cookie;
                            break;
                        }
                    }
                    if (c == null) {
                        response.sendRedirect("login.jsp");
                        System.out.println("未找到cookie'info'");
                    } else {
                        System.out.println("找到cookie,读取中...");
                        String value = c.getValue();
                        String[] values = value.split(":");
                        UserService userService=new UserService();
                        Map<String, Object> item=userService.checkLogin(values[0],values[1]);
                        if (item==null){
                            response.sendRedirect("login.jsp");
                            System.out.println("cookies账户名密码验证失败!");
                            chain.doFilter(req, resp);
                        }else {
                            System.out.println("cookies账户名密码验证成功!!!");
                            session.setAttribute("user",item);
                            chain.doFilter(req, resp);
                        }

                    }
            }

    }
    @Override
    public void init(FilterConfig config) throws ServletException {
/*        *//**
         * 初始化配置参数
         * @redirectURL :
         *//*

        redirectURL = config.getInitParameter("redirectURL");

        String notCheckURLStr = config.getInitParameter("notCheckURLList");

        if(notCheckURLStr != null) {

            notCheckURLList = new ArrayList<String>();

            notCheckURLList.clear();

            String[] urls = notCheckURLStr.split(",");

            for (String url : urls) {

                notCheckURLList.add(url);

            }

        }*/
    }

}
